#ifndef HUFFMAN_H
#define HUFFMAN_H

#define MAX_CHAR 128   // 最大字符数
#define MAX_TREE_SIZE (2 * MAX_CHAR - 1)
#define MAX_CODE_LEN 128

// ================= 动态存储结构 =================
typedef struct DNode {
    char ch;
    int weight;
    struct DNode *left, *right;
} DNode, *DTree;

// ================= 静态存储结构 =================
typedef struct {
    char ch;
    int weight;
    int parent, left, right;
} SNode, STree[MAX_TREE_SIZE];

// ================= 编码表 =================
typedef struct {
    char ch;
    char code[MAX_CODE_LEN];
} CodeTable;

// ====== 动态结构函数 ======
DTree buildDTree(char chs[], int w[], int n);
void getDCodes(DTree root, CodeTable table[], int n);
void printDTree(DTree root, int depth);
void freeDTree(DTree root);
void encodeD(DTree root, char *str, char *res);
void decodeD(DTree root, char *code, char *res);

// ====== 静态结构函数 ======
int buildSTree(char chs[], int w[], int n, STree tree);
void getSCodes(STree tree, int n, CodeTable table[]);
void printSTree(STree tree, int n, int root, int depth);
void encodeS(STree tree, int n, char *str, char *res);
void decodeS(STree tree, int n, char *code, char *res);

#endif